package com.swt.mto.thithu.portlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.swt.mto.thithu.model.LoadAjax;
import com.swt.mto.thithu.model.Quiz;
import com.swt.mto.thithu.model.QuizCategory;
import com.swt.mto.thithu.service.LoadAjaxLocalServiceUtil;
import com.swt.mto.thithu.service.QuizCategoryLocalServiceUtil;
import com.swt.mto.thithu.service.QuizLocalServiceUtil;

/**
 * Portlet implementation class QuanLyCauHoiPortlet
 */
public class QuanLyCauHoiPortlet extends MVCPortlet {

	public void serveResource(ResourceRequest resourceRequest,
			ResourceResponse resourceResponse) throws PortletException,
			IOException {
		ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest
				.getAttribute(WebKeys.THEME_DISPLAY);
		if (resourceRequest.getResourceID().equals("new-question-catalogy")) {
		
			QuizCategory quizCategory = null;
			String strParentId = resourceRequest.getParameter("parentId");
			Long parentId=null;
			if(strParentId == null){
				parentId = (long) 0;
			}else{
				strParentId = strParentId.replace("c", "");
				parentId = Long.parseLong(strParentId);
			}
			try {
				quizCategory = QuizCategoryLocalServiceUtil
						.addQuizCategory(null, themeDisplay.getUserId(), null, parentId, "New Category");
			} catch (PortalException e) {
				e.printStackTrace();
			} catch (SystemException e) {
				e.printStackTrace();
			}
			
			String str ="c"+ quizCategory.getParentId()+ "!@#<li rel = 'folder' id='c"+ quizCategory.getQuizCategoryId() +"' class='jstree-closed jstree-last c"+quizCategory.getParentId()+"'>"; 
			str+=			"<ins class='jstree-icon'>&nbsp;</ins>";
			str+=			"<a href='javascript:;' class='jstree-clicked'><ins class='jstree-icon'>&nbsp;</ins>" + quizCategory.getTitle() + "</a>";
			str+=		"</li>!@#c"+quizCategory.getQuizCategoryId();
			resourceResponse.setContentType("text/html; charset=UTF-8");

			PrintWriter writer = resourceResponse.getWriter();
			writer.write(str);
					
		}
		
		if (resourceRequest.getResourceID().equals("load")){
			
			LoadAjax loadAjax;
			try {
				loadAjax = LoadAjaxLocalServiceUtil.fetchLoadAjax(1);
					
				resourceResponse.setContentType("text/html; charset=UTF-8");
				PrintWriter writer = resourceResponse.getWriter();
				writer.write(loadAjax.getLoadWeb());
			} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if (resourceRequest.getResourceID().equals("saveTree")){
			LoadAjax loadAjax;
			try {
				loadAjax = LoadAjaxLocalServiceUtil.fetchLoadAjax(1);
				String path = resourceRequest.getParameter("saveTreePara");
				loadAjax.setLoadWeb(path);
				LoadAjaxLocalServiceUtil.updateLoadAjax(loadAjax);
			} catch (SystemException e) {
				e.printStackTrace();
			}
		}
		
		if (resourceRequest.getResourceID().equals("update-quiz-catalogy")){
			QuizCategory quizCategory = null;
			String strQuizCategoryId = resourceRequest.getParameter("quizCategoryId");
			String title = resourceRequest.getParameter("title");
			Long quizCategoryId=null;
			if(strQuizCategoryId == null){
				quizCategoryId = (long) 0;
			}else{
				strQuizCategoryId = strQuizCategoryId.replace("c", "");
				quizCategoryId = Long.parseLong(strQuizCategoryId);
			}
			try {
				Long createUserId = QuizCategoryLocalServiceUtil.fetchQuizCategory(quizCategoryId).getCreateUserId();
				Long parentId = QuizCategoryLocalServiceUtil.fetchQuizCategory(quizCategoryId).getParentId();
				quizCategory = QuizCategoryLocalServiceUtil
						.addQuizCategory(quizCategoryId, createUserId, themeDisplay.getUserId(), parentId, title);
			} catch (PortalException e) {
				e.printStackTrace();
			} catch (SystemException e) {
				e.printStackTrace();
			}
			
			resourceResponse.setContentType("application/json; charset=UTF-8");
			JSONObject json = JSONFactoryUtil.createJSONObject();
			json.put("id", quizCategory.getQuizCategoryId());
			json.put("title",quizCategory.getTitle());
			resourceResponse.getWriter().write(json.toString());
		}
		
		if (resourceRequest.getResourceID().equals("remove-quiz-catalogy")){
			String strQuizCategoryId = resourceRequest.getParameter("quizCategoryId");
			strQuizCategoryId = strQuizCategoryId.replace("c", "");
			Long quizCategoryId = Long.parseLong(strQuizCategoryId);
			try {
				QuizCategoryLocalServiceUtil.deleteQuizCategory(quizCategoryId);
				
			} catch (PortalException e) {
				e.printStackTrace();
			} catch (SystemException e) {
				e.printStackTrace();
			}
			
			
			
			resourceResponse.setContentType("application/json; charset=UTF-8");
			JSONObject json = JSONFactoryUtil.createJSONObject();
			json.put("id", resourceRequest.getParameter("quizCategoryId"));
			resourceResponse.getWriter().write(json.toString());
		}
		
		if (resourceRequest.getResourceID().equals("new-quiz")) {
			
			Quiz quiz = null;
			String strQuizCategoryId = resourceRequest.getParameter("parentId");
			Long QuizCategoryId=null;
			if(strQuizCategoryId == null){
				QuizCategoryId = (long) 0;
			}else{
				strQuizCategoryId = strQuizCategoryId.replace("c", "");
				QuizCategoryId = Long.parseLong(strQuizCategoryId);
			}
			try {
				quiz = QuizLocalServiceUtil
						.addQuiz(null, themeDisplay.getUserId(), null, QuizCategoryId, "New Quiz", 0,"");
			} catch (PortalException e) {
				e.printStackTrace();
			} catch (SystemException e) {
				e.printStackTrace();
			}
			
			String str ="c"+ quiz.getQuizCategoryId()+ "!@#<li rel = 'quiz' id='b"+ quiz.getQuizId() +"' class='jstree-closed jstree-last c"+quiz.getQuizCategoryId()+"'>"; 
			str+=			"<ins class='jstree-icon'>&nbsp;</ins>";
			str+=			"<a href='javascript:;' class='jstree-clicked'><ins class='jstree-icon'>&nbsp;</ins>" + quiz.getTitle() + "</a>";
			str+=			"<input type='hidden' value='" + quiz.getTime() + "' />";
			str+=			"<input type='hidden' value='" + quiz.getDescription() + "' />";
			str+=		"</li>!@#b"+quiz.getQuizId();
			resourceResponse.setContentType("text/html; charset=UTF-8");

			PrintWriter writer = resourceResponse.getWriter();
			writer.write(str);
					
		}
		
		if (resourceRequest.getResourceID().equals("update-quiz")){
			Quiz quiz = null;
			String strQuizId = resourceRequest.getParameter("quizId");
			String title = resourceRequest.getParameter("title");
			Long quizId=null;
			if(strQuizId == null){
				quizId = (long) 0;
			}else{
				strQuizId = strQuizId.replace("b", "");
				quizId = Long.parseLong(strQuizId);
			}
			
			int time = Integer.parseInt(resourceRequest.getParameter("time"));
			String description = resourceRequest.getParameter("description");
			
			try {
				Long createUserId = QuizLocalServiceUtil.fetchQuiz(quizId).getCreateUserId();
				Long quizCategoryId = QuizLocalServiceUtil.fetchQuiz(quizId).getQuizCategoryId();
				quiz = QuizLocalServiceUtil
						.addQuiz(quizId, createUserId, themeDisplay.getUserId(), quizCategoryId, title, time, description);
			} catch (PortalException e) {
				e.printStackTrace();
			} catch (SystemException e) {
				e.printStackTrace();
			}
			
			resourceResponse.setContentType("application/json; charset=UTF-8");
			JSONObject json = JSONFactoryUtil.createJSONObject();
			json.put("id", quiz.getQuizId());
			json.put("title",quiz.getTitle());
			json.put("test","yes");
			json.put("time",quiz.getTime());
			json.put("description",quiz.getDescription());
			resourceResponse.getWriter().write(json.toString());
		}
		
		
		if (resourceRequest.getResourceID().equals("remove-quiz")){
			String strQuizId = resourceRequest.getParameter("quizId");
			strQuizId = strQuizId.replace("b", "");
			Long quizId = Long.parseLong(strQuizId);
			try {
				QuizLocalServiceUtil.deleteQuiz(quizId);
				
			} catch (PortalException e) {
				e.printStackTrace();
			} catch (SystemException e) {
				e.printStackTrace();
			}
			
			
			
			resourceResponse.setContentType("application/json; charset=UTF-8");
			JSONObject json = JSONFactoryUtil.createJSONObject();
			json.put("id", resourceRequest.getParameter("quizId"));
			json.put("test", "yes");
			resourceResponse.getWriter().write(json.toString());
		}
	}
}
